package com.atguigu.gulimall.product.controller;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimall.product.service.AttrGroupService;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.AttrGroupRelationVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 属性分组
 *
 * @author leifengyang
 * @email leifengyang@gmail.com
 * @date 2019-10-01 22:50:32
 */
@RestController
@RequestMapping("product/attrgroup")
@Slf4j
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    AttrService attrService;

    @Autowired
    AttrAttrgroupRelationService relationService;


    /**
     * 修改属性接口分组
     * @param attrGroupId
     * @return
     */
    @GetMapping("/{attrGroupId}")
    public R getAttrGroupRelation(@PathVariable("attrGroupId") Long attrGroupId){
        AttrGroupEntity attrGroupEntity = attrGroupService.getById(attrGroupId);
        Long[] catelogIds = categoryService.findCategoryPath(attrGroupEntity.getCatelogId());
        attrGroupEntity.setCatelogIds(catelogIds);
        return R.ok().put("data",attrGroupEntity);
    }

    @DeleteMapping("/{attrGroupId}")
    public R deleteRelation(@PathVariable("attrGroupId") Long attrGroupId){
        attrGroupService.removeById(attrGroupId);
        return R.ok();
    }

    ///product/attrgroup/attr/relation
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> vos) {

        relationService.saveBatch(vos);
        return R.ok();
    }

    ///product/attrgroup/{catelogId}/withattr
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId) {

        //1、查出当前分类下的所有属性分组，
        //2、查出每个属性分组的所有属性

        return R.ok();
    }


    ///product/attrgroup/{attrgroupId}/attr/relation
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId) {

        return R.ok();
    }

    ///product/attrgroup/{attrgroupId}/noattr/relation
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,
                            @RequestParam Map<String, Object> params) {

        return R.ok();
    }

    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] vos) {

        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params) {
//        PageUtils page = attrGroupService.queryPage(params);
        Long catelogId = null;
        Object catelogIdObj = params.get("catelogId");
        if (catelogIdObj != null) {
            if (catelogIdObj instanceof Long) {
                catelogId = (Long) catelogIdObj;
            } else {
                catelogId = Long.parseLong(catelogIdObj.toString());
            }
        }
        PageUtils pageUtils = attrGroupService.queryPage(params, catelogId);

        return R.ok().put("data", pageUtils);
    }


    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        if (attrGroup.getCatelogIds() != null) {
            attrGroup.setCatelogId(attrGroup.getCatelogIds()[attrGroup.getCatelogIds().length - 1]);
        }
        attrGroupService.saveOrUpdate(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
